﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MeTalker.Aide.data;
using Top.Api;
using MeTalker.Securitys.Cryptography;
using System.Diagnostics;

namespace MeTalker.Aide
{
    internal class UtilHelper
    {
        internal static readonly string TAOBAO_SESSION_API_URL = "http://container.open.taobao.com/container?authcode={0}";
        internal static readonly string TAOBAO_CODE_API_URL = "http://my.open.taobao.com/auth/authorize.htm?appkey={0}";
        //internal static readonly string TAOBAO_URL = "http://gw.api.tbsandbox.com/router/rest";//沙箱环境调用地址,
        internal static readonly string TAOBAO_API_URL = "http://gw.api.taobao.com/router/rest";//正式环境需要设置为:
        internal static readonly string TAOBAO_QUERY_ORDER_WAIT_PAYMENT_URL = "http://trade.taobao.com/trade/itemlist/list_sold_items.htm?user_type=1&event_submit_do_query=1&action=itemlist/query_action&_fmt.q._0.au=NOT_PAID&_fmt.q._0.b=";
        internal static readonly string TAOBAO_QUERY_ORDER_URL = "http://trade.taobao.com/trade/itemlist/list_sold_items.htm?user_type=1&event_submit_do_query=1&action=itemlist/query_action&_fmt.q._0.b=";
        internal static readonly string TAOBAO_QUERY_ORDER_WAIT_DELIVERY_URL = "http://trade.taobao.com/trade/itemlist/list_sold_items.htm?user_type=1&event_submit_do_query=1&action=itemlist/query_action&_fmt.q._0.au=PAID&_fmt.q._0.b=";
        //http://trade.taobao.com/trade/itemlist/list_sold_items.htm?user_type=1&event_submit_do_query=1&action=itemlist/query_action&_fmt.q._0.b=yan168138
        internal static readonly string TYPE_USER_LOGOUT = "-1";//请求注销
        internal static readonly string TYPE_USER_LOGIN = "1";//请求登录
        internal static readonly string TYPE_USER_HEART = "2";//请求心跳
        internal static readonly string TYPE_USER_LICENSE = "3";//请求许可
        internal static readonly string TYPE_USER_REG = "4";//请求注册
        internal static readonly string SYSTEM_FAIL = "-99";
        internal static readonly string LOGIN_FAIL = "-100";
        internal static readonly string LOGIN_OVER_VALID = "-101";
        internal static readonly string LOGIN_USER_EXIST = "-102";
        internal static readonly string USER_INACTIVE_ACTIVATE = "-103";
        internal static readonly string USER_INVALID = "-104";
        internal static readonly string LOGIN_DOUBLE = "-105";
        internal static readonly string REG_DOUBLE = "-106";
        internal static readonly string USER_EXIST = "-107";
        internal static readonly string USER_NOT_EXIST = "-108";
        internal static readonly string CONST_FLAG = "jlkl2008";
        internal static readonly string SYSTEM_MSG_STR = "系统消息";
       
        internal static readonly string TAOBAO_TOP_SESSION_STR = "top_session";

        internal static readonly int CHECK_USER_INTERVAL = 1000;
        internal static readonly int HEART_COUNT_INTERVAL = 200;
        //internal static readonly string BASE_URL = "http://localhost:3671/WebLogin/";
        //internal const string BASE_URL = "http://localhost:3671/WebLogin/";
        internal const string BASE_URL = "http://kinglansoft.w12.mc-test.com/";
        internal static readonly string HELP_BASE_URL = "http://www.jinlansoft.com/news/";

        internal static readonly string LOGIN_URL = BASE_URL + "RequestLoginHandler.ashx?type={0}&dataType=2&soft=JL0001&random={1}";
        internal static readonly string HEART_URL = BASE_URL + "RequestLoginHandler.ashx?type={0}&dataType=2&sid={1}&soft=JL0001&random={2}";
        internal static readonly string UPDATE_LICENSE_URL = BASE_URL + "RequestLoginHandler.ashx?type={0}&dataType=2&sid={1}&soft=JL0001&random={2}&un={3}";
        //internal const string USER_REG_URL = BASE_URL + "RegistrPage.aspx?type={0}&DataType=2&if={1}&soft=JL0001&random={2}";
        internal static readonly string USER_REG_URL = BASE_URL + "RequestLoginHandler.ashx?type={0}&dataType=2&soft=JL0001&random={1}";

        internal const string TITLE = "--客服工作台";
        internal const string TITLE_MAIN = "-阿里旺旺2011卖家版";
        internal const string TITLE_MAIN_2012 = "-阿里旺旺2012卖家版";
        internal const string IME_REPLY = ",.";
        internal const string IME_SET = ",,";
        internal const string FAST_RIGHT_SYMBOL = "】";
        internal const string FAST_LEFT_SYMBOL = "【";
        internal const int MAX_ORDER_HEIGHT = 150;
        internal static int MAX_MSG_WIDTH = 320;
        internal readonly static int MIN_MSG_WIDTH = 160;
        internal const int MAX_LIST_COUNT = 6;
        internal static bool DEBUG { get; private set; }// = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["DEBUG"]);
        const int TIME_INTERVAL = 30;
        internal const int SEND_TIME_OUT = 300;
        internal const int MAX_COUNT = 100;
        internal const int CHECK_TIME_OUT = 100;
        internal static DateTime baseDateTime = DateTime.Now;

        internal static void RunBrowser(string url)
        {
            try
            {
                RunBrowser(url, false);
            }
            catch
            {
            }
        }

        internal static void RunBrowser(string url, bool isuser)
        {
            try
            {
                if (isuser)
                {
                    Process.Start("zbrowser.exe", url);
                }
                else
                {
                    Process.Start(url);
                }
            }
            catch
            {
            }
        }
        internal static IDictionary<string, string> GetParam(string str, char split)
        {
            IDictionary<string, string> idConfigs = new Dictionary<string, string>();
            string[] configs = str.Split(new char[] { split });
            foreach (var val in configs)
            {
                string[] vals = val.Split(new char[] { '=' });
                if (vals.Length >= 2)
                {
                    idConfigs.Add(vals[0], vals[1]);
                }
            }
            return idConfigs;
        }

        internal static IDictionary<string, string> GetParamTopSession(string str)
        {
           return GetParam(str, '&');
        }

        internal static string SplitMsg(string msg)
        {
            try {
                int len = msg.Length;
                if (len > 3) {
                    bool flag = true;
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < len; i += 2)
                    {
                        if (flag)
                        {
                            sb.Append(msg.Substring(i, 2));
                            flag = false;
                        }
                        else
                        {
                            if (i + 2 > len)
                            {
                                sb.Append( ","+msg.Substring(i, 1));
                            }
                            else
                            {
                                sb.Append("," + msg.Substring(i, 2));
                            }
                        }
                    }
                    return sb.ToString();
                }
                return msg;
            }
            catch {
                return msg;
            }
        }

        internal static string GetSubText(string msg, int index, int length)
        {
            return msg.Substring(index, msg.Length - length);
        }

        internal static int GetDay(DateTime nowTime, DateTime endTime)
        {
            TimeSpan span = nowTime.Subtract(endTime);
            return span.Days + 1;
        }

        internal static void Update(string userId, string pwd, string sessionId, bool flag)
        {
            if (!flag) {
                userId = ""; pwd = "";
            }
            FileFactory.WriterFile(FileFactory.BaseDirectory + "user.dat", new AESHelper().Encrypt(Encoding.UTF8.GetBytes(string.Format("u={0},p={1},s={2},v={3}", userId, pwd, sessionId, flag == true ? "true" : "false"))));
        }

        internal static void Update()
        {
            try
            {
                if (!DataFactory.User.IsSave)
                {
                    FileFactory.WriterFile(FileFactory.BaseDirectory + "user.dat", new AESHelper().Encrypt(Encoding.UTF8.GetBytes(string.Format("u={0},p={1},s={2},v={3}", "", "", DataFactory.User.SessionId, DataFactory.User.IsSave == true ? "true" : "false"))));
                }
                else
                {
                    FileFactory.WriterFile(FileFactory.BaseDirectory + "user.dat", new AESHelper().Encrypt(Encoding.UTF8.GetBytes(string.Format("u={0},p={1},s={2},v={3}", DataFactory.User.UserName, DataFactory.User.Passwd, DataFactory.User.SessionId, DataFactory.User.IsSave == true ? "true" : "false"))));
                }
            }
            catch { }
        }

        internal static MessageBody FormatRequestMessage(string text, string account)
        {
            string[] msgs = text.Replace(" ", "").Split(new char[] { '\r', '\n' });
            int len = msgs.Length;
            string msg_name = msgs[len - 3];
            string msg = msgs[len - 1];
            string SendName = msg_name.Substring(0, msg_name.IndexOf("("));
            string ReceivTime = msg_name.Replace(SendName, "").Replace("(", "").Replace(")", "");
            ReceivTime = ReceivTime.Substring(0, ReceivTime.Length - 1);
            //if (account.Equals(SendName))
            //    return null;
            MessageBody msgBody = new MessageBody();
            msgBody.MsgTime = ReceivTime;
            msgBody.SendName = SendName;
            msgBody.Content = msg;
            return msgBody;
        }

        static UtilHelper()
        {
            bool debug;
            bool.TryParse(System.Configuration.ConfigurationManager.AppSettings["DEBUG"], out debug);
            DEBUG = debug;
        }

        internal static string FormatMessage(string msg)
        {
            if (msg != null)
                return msg.Replace("，。", ",.").Replace("，.", ",.").Replace(",。", ",.").Replace("，，", ",,").Replace("，,", ",,").Replace(",，", ",,");
            return "";
        }

        #region 查找答案

        internal static string FindAnswer(string msg)
        {
            try
            {
                var ds = from temp in DataFactory.DbAnswers where temp.Problem.Contains(msg) select temp;
                foreach (var d in ds)
                {
                    string[] dd = d.Problem.Split(new char[] { '\r', '\n' });
                    foreach (var str in dd)
                    {
                        if (msg.Equals(str))
                        {
                            return d.Answer;
                        }
                    }
                }
                #region 大
                //foreach (var dd in DataFactory.Answers.Nodes)
                //{
                //    foreach (var dd1 in dd.SubNode)
                //    {
                //        if (dd1.SubNode.Count > 0)
                //        {
                //            #region 1
                //            foreach (var dd2 in dd1.SubNode)
                //            {
                //                if (dd2.SubNode.Count > 0)
                //                {
                //                    #region 2
                //                    foreach (var dd3 in dd2.SubNode)
                //                    {
                //                        if (dd3.SubNode.Count > 0)
                //                        {
                //                            #region 2
                //                            foreach (var dd4 in dd3.SubNode)
                //                            {
                //                                if (dd4.SubNode.Count > 0)
                //                                {

                //                                }
                //                                else
                //                                {
                //                                    if (msg.Equals(dd4.Key))
                //                                    {
                //                                        return dd4.Text;
                //                                    }
                //                                }
                //                            }
                //                            #endregion
                //                        }
                //                        else
                //                        {
                //                            if (msg.Equals(dd3.Key))
                //                            {
                //                                return dd3.Text;
                //                            }
                //                        }
                //                    }
                //                    #endregion
                //                }
                //                else
                //                {
                //                    if (msg.Equals(dd2.Key))
                //                    {
                //                        return dd2.Text;
                //                    }
                //                }
                //            }
                //            #endregion
                //        }
                //        else
                //        {
                //            if (msg.Equals(dd1.Key))
                //            {
                //                return dd1.Text;
                //            }
                //        }
                //    }
                //    if (msg.Equals(dd.Key))
                //    {
                //        return dd.Text;
                //    }
                //}
                #endregion

            }
            catch { }
            return string.Empty;
        }

        internal static List<Record> FindLinkAnswer(string msg)
        {
            try
            {
                var ds = from temp in DataFactory.ChatRecords.Records where temp.Words.Contains(msg) select temp;
                //var ds = from temp in DataFactory.AnswerProblem.Records where temp.Words.Contains(msg) select temp;
                List<Record> records = ds.ToList();
                FindWordMessages(records, msg);
                return records;
            }
            catch { }
            return null;
        }

        static bool IsKey(Node temp1, string msg)
        {
            try
            {
                Node node = temp1;
                bool flag = false;
                if (string.IsNullOrEmpty(node.Keyword1))
                    return false;
                string[] keys = node.Keyword1.Split(',');
                foreach (string s in keys)
                {
                    if (msg.Contains(s))
                    {
                        flag = true;
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword2))
                {
                    flag = false;
                    keys = node.Keyword2.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword3))
                {
                    flag = false;
                    keys = node.Keyword3.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword4))
                {
                    flag = false;
                    keys = node.Keyword4.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword5))
                {
                    flag = false;
                    keys = node.Keyword5.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword6))
                {
                    flag = false;
                    keys = node.Keyword6.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword7))
                {
                    flag = false;
                    keys = node.Keyword7.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                    return false;
                if (!string.IsNullOrEmpty(node.Keyword8))
                {
                    flag = false;
                    keys = node.Keyword8.Split(',');
                    foreach (string s in keys)
                    {
                        if (msg.Contains(s))
                        {
                            flag = true;
                        }
                    }
                }
                return flag;
            }
            catch
            {
                return false;
            }
        }

        internal static List<Record> FindWordMessages(List<Record> temp1, string msg)
        {
            try
            {
                foreach (var dd in DataFactory.Messages.Nodes)
                {
                    foreach (var dd1 in dd.SubNode)
                    {
                        if (dd1.SubNode.Count > 0)
                        {
                            #region 1
                            foreach (var dd2 in dd1.SubNode)
                            {
                                if (dd2.SubNode.Count > 0)
                                {
                                    #region 2
                                    foreach (var dd3 in dd2.SubNode)
                                    {
                                        if (dd3.SubNode.Count > 0)
                                        {
                                            #region 3
                                            foreach (var dd4 in dd3.SubNode)
                                            {
                                                if (dd4.SubNode.Count > 0)
                                                {
                                                    #region 4
                                                    foreach (var dd5 in dd4.SubNode)
                                                    {
                                                        if (dd5.SubNode.Count > 0)
                                                        {
                                                            #region 5
                                                            foreach (var dd6 in dd5.SubNode)
                                                            {
                                                                if (dd6.SubNode.Count > 0)
                                                                {
                                                                    #region 6
                                                                    foreach (var dd7 in dd6.SubNode)
                                                                    {
                                                                        if (dd7.SubNode.Count > 0)
                                                                        {
                                                                            #region 7
                                                                            foreach (var dd8 in dd7.SubNode)
                                                                            {
                                                                                if (dd8.SubNode.Count > 0)
                                                                                {
                                                                                    #region 8
                                                                                    foreach (var dd9 in dd8.SubNode)
                                                                                    {
                                                                                        if (dd9.SubNode.Count > 0)
                                                                                        {
                                                                                            #region 9
                                                                                            foreach (var dd10 in dd9.SubNode)
                                                                                            {
                                                                                                if (dd10.SubNode.Count > 0)
                                                                                                {
                                                                                                           #region 10
                                                                                                    foreach (var dd11 in dd10.SubNode)
                                                                                                    {
                                                                                                        if (dd11.SubNode.Count > 0)
                                                                                                        {
                                                                                                            #region 10
                                                                                                            foreach (var dd12 in dd11.SubNode)
                                                                                                            {
                                                                                                                if (dd12.SubNode.Count > 0)
                                                                                                                {
                                                                                                                    //temp1.Add(new Record()
                                                                                                                    //{
                                                                                                                    //    Text = temp.Text
                                                                                                                    //});
                                                                                                                }
                                                                                                                else
                                                                                                                {
                                                                                                                    if (IsKey(dd12, msg))
                                                                                                                    {
                                                                                                                        temp1.Add(new Record()
                                                                                                                        {
                                                                                                                            Text = dd12.Text
                                                                                                                        });
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            #endregion
                                                                                                        }
                                                                                                        else
                                                                                                        {
                                                                                                            if (IsKey(dd11, msg))
                                                                                                            {
                                                                                                                temp1.Add(new Record()
                                                                                                                {
                                                                                                                    Text = dd11.Text
                                                                                                                });
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    #endregion
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (IsKey(dd10, msg))
                                                                                                    {
                                                                                                        temp1.Add(new Record()
                                                                                                        {
                                                                                                            Text = dd10.Text
                                                                                                        });
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                            #endregion
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (IsKey(dd9, msg))
                                                                                            {
                                                                                                temp1.Add(new Record()
                                                                                                {
                                                                                                    Text = dd9.Text
                                                                                                });
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    #endregion
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (IsKey(dd8, msg))
                                                                                    {
                                                                                        temp1.Add(new Record()
                                                                                        {
                                                                                            Text = dd8.Text
                                                                                        });
                                                                                    }
                                                                                }
                                                                            }
                                                                            #endregion
                                                                        }
                                                                        else
                                                                        {
                                                                            if (IsKey(dd7, msg))
                                                                            {
                                                                                temp1.Add(new Record()
                                                                                {
                                                                                    Text = dd7.Text
                                                                                });
                                                                            }
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    if (IsKey(dd6, msg))
                                                                    {
                                                                        temp1.Add(new Record()
                                                                        {
                                                                            Text = dd6.Text
                                                                        });
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                        }
                                                        else
                                                        {
                                                            if (IsKey(dd5, msg))
                                                            {
                                                                temp1.Add(new Record()
                                                                {
                                                                    Text = dd5.Text
                                                                });
                                                            }
                                                        }
                                                    }
                                                    #endregion
                                                }
                                                else
                                                {
                                                    if (IsKey(dd4, msg))
                                                    {
                                                        temp1.Add(new Record()
                                                        {
                                                            Text = dd4.Text
                                                        });
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            if (IsKey(dd3, msg))
                                            {
                                                temp1.Add(new Record()
                                                {
                                                    Text = dd3.Text
                                                });
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    if (IsKey(dd2, msg))
                                    {
                                        temp1.Add(new Record()
                                        {
                                            Text = dd2.Text
                                        });
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            if (IsKey(dd1, msg))
                            {
                                temp1.Add(new Record()
                                {
                                    Text = dd1.Text
                                });
                            }
                        }
                    }
                }
            }
            catch { }
            return temp1;
        }

        #endregion

        internal static List<Record> FindMessages(string msg)
        {
            List<Record> temp1 = new List<Record>();
            try
            {
                foreach (var dd in DataFactory.Messages.Nodes)
                {
                    foreach (var dd1 in dd.SubNode)
                    {
                        if (dd1.SubNode.Count > 0)
                        {
                            #region 1
                            foreach (var dd2 in dd1.SubNode)
                            {
                                if (dd2.SubNode.Count > 0)
                                {
                                    #region 2
                                    foreach (var dd3 in dd2.SubNode)
                                    {
                                        if (dd3.SubNode.Count > 0)
                                        {
                                            #region 3
                                            foreach (var dd4 in dd3.SubNode)
                                            {
                                                if (dd4.SubNode.Count > 0)
                                                {
                                                    #region 4
                                                    foreach (var dd5 in dd4.SubNode)
                                                    {
                                                        if (dd5.SubNode.Count > 0)
                                                        {
                                                            #region 5
                                                            foreach (var dd6 in dd5.SubNode)
                                                            {
                                                                if (dd6.SubNode.Count > 0)
                                                                {
                                                                    #region 6
                                                                    foreach (var dd7 in dd6.SubNode)
                                                                    {
                                                                        if (dd7.SubNode.Count > 0)
                                                                        {
                                                                            #region 7
                                                                            foreach (var dd8 in dd7.SubNode)
                                                                            {
                                                                                if (dd8.SubNode.Count > 0)
                                                                                {
                                                                                    #region 8
                                                                                    foreach (var dd9 in dd8.SubNode)
                                                                                    {
                                                                                        if (dd9.SubNode.Count > 0)
                                                                                        {
                                                                                            #region 9
                                                                                            foreach (var dd10 in dd9.SubNode)
                                                                                            {
                                                                                                if (dd10.SubNode.Count > 0)
                                                                                                {
                                                                                                    #region 10
                                                                                                    foreach (var dd11 in dd10.SubNode)
                                                                                                    {
                                                                                                        if (dd11.SubNode.Count > 0)
                                                                                                        {
                                                                                                            #region 10
                                                                                                            foreach (var dd12 in dd11.SubNode)
                                                                                                            {
                                                                                                                if (dd12.SubNode.Count > 0)
                                                                                                                {
                                                                                                                    //temp1.Add(new Record()
                                                                                                                    //{
                                                                                                                    //    Text = temp.Text
                                                                                                                    //});
                                                                                                                }
                                                                                                                else
                                                                                                                {
                                                                                                                    if (dd12.Text.Contains(msg))
                                                                                                                    {
                                                                                                                        temp1.Add(new Record()
                                                                                                                        {
                                                                                                                            Text = dd12.Text
                                                                                                                        });
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            #endregion
                                                                                                        }
                                                                                                        else
                                                                                                        {
                                                                                                            if (dd11.Text.Contains(msg))
                                                                                                            {
                                                                                                                temp1.Add(new Record()
                                                                                                                {
                                                                                                                    Text = dd11.Text
                                                                                                                });
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    #endregion
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (dd10.Text.Contains(msg))
                                                                                                    {
                                                                                                        temp1.Add(new Record()
                                                                                                        {
                                                                                                            Text = dd10.Text
                                                                                                        });
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                            #endregion
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (dd9.Text.Contains(msg))
                                                                                            {
                                                                                                temp1.Add(new Record()
                                                                                                {
                                                                                                    Text = dd9.Text
                                                                                                });
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    #endregion
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (dd8.Text.Contains(msg))
                                                                                    {
                                                                                        temp1.Add(new Record()
                                                                                        {
                                                                                            Text = dd8.Text
                                                                                        });
                                                                                    }
                                                                                }
                                                                            }
                                                                            #endregion
                                                                        }
                                                                        else
                                                                        {
                                                                            if (dd7.Text.Contains(msg))
                                                                            {
                                                                                temp1.Add(new Record()
                                                                                {
                                                                                    Text = dd7.Text
                                                                                });
                                                                            }
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    if (dd6.Text.Contains(msg))
                                                                    {
                                                                        temp1.Add(new Record()
                                                                        {
                                                                            Text = dd6.Text
                                                                        });
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                        }
                                                        else
                                                        {
                                                            if (dd5.Text.Contains(msg))
                                                            {
                                                                temp1.Add(new Record()
                                                                {
                                                                    Text = dd5.Text
                                                                });
                                                            }
                                                        }
                                                    }
                                                    #endregion
                                                }
                                                else
                                                {
                                                    if (dd4.Text.Contains(msg))
                                                    {
                                                        temp1.Add(new Record()
                                                        {
                                                            Text = dd4.Text
                                                        });
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            if (dd3.Text.Contains(msg))
                                            {
                                                temp1.Add(new Record()
                                                {
                                                    Text = dd3.Text
                                                });
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    if (dd2.Text.Contains(msg))
                                    {
                                        temp1.Add(new Record()
                                        {
                                            Text = dd2.Text
                                        });
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            if (dd1.Text.Contains(msg))
                            {
                                temp1.Add(new Record()
                                {
                                    Text = dd1.Text
                                });
                            }
                        }
                    }
                }
            }
            catch { }
            return temp1;
        }

        internal static string FindEqualMessages(string msg)
        {
            try
            {
                foreach (var dd in DataFactory.Messages.Nodes)
                {
                    foreach (var dd1 in dd.SubNode)
                    {
                        if (dd1.SubNode.Count > 0)
                        {
                            #region 1
                            foreach (var dd2 in dd1.SubNode)
                            {
                                if (dd2.SubNode.Count > 0)
                                {
                                    #region 2
                                    foreach (var dd3 in dd2.SubNode)
                                    {
                                        if (dd3.SubNode.Count > 0)
                                        {
                                            #region 3
                                            foreach (var dd4 in dd3.SubNode)
                                            {
                                                if (dd4.SubNode.Count > 0)
                                                {
                                                    #region 4
                                                    foreach (var dd5 in dd4.SubNode)
                                                    {
                                                        if (dd5.SubNode.Count > 0)
                                                        {
                                                            #region 5
                                                            foreach (var dd6 in dd5.SubNode)
                                                            {
                                                                if (dd6.SubNode.Count > 0)
                                                                {
                                                                    #region 6
                                                                    foreach (var dd7 in dd6.SubNode)
                                                                    {
                                                                        if (dd7.SubNode.Count > 0)
                                                                        {
                                                                            #region 7
                                                                            foreach (var dd8 in dd7.SubNode)
                                                                            {
                                                                                if (dd8.SubNode.Count > 0)
                                                                                {
                                                                                    #region 8
                                                                                    foreach (var dd9 in dd8.SubNode)
                                                                                    {
                                                                                        if (dd9.SubNode.Count > 0)
                                                                                        {
                                                                                            #region 9
                                                                                            foreach (var dd10 in dd9.SubNode)
                                                                                            {
                                                                                                if (dd10.SubNode.Count > 0)
                                                                                                {
                                                                                                    #region 10
                                                                                                    foreach (var dd11 in dd10.SubNode)
                                                                                                    {
                                                                                                        if (dd11.SubNode.Count > 0)
                                                                                                        {
                                                                                                            #region 11
                                                                                                            foreach (var dd12 in dd11.SubNode)
                                                                                                            {
                                                                                                                if (dd12.SubNode.Count > 0)
                                                                                                                {
                                                                                                                    //temp1.Add(new Record()
                                                                                                                    //{
                                                                                                                    //    Text = temp.Text
                                                                                                                    //});
                                                                                                                }
                                                                                                                else
                                                                                                                {
                                                                                                                    if (dd12.Text.Equals(msg))
                                                                                                                    {
                                                                                                                        return dd12.Text;
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            #endregion
                                                                                                        }
                                                                                                        else
                                                                                                        {
                                                                                                            if (dd11.Text.Equals(msg))
                                                                                                            {
                                                                                                                return dd11.Text;
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    #endregion
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (dd10.Text.Equals(msg))
                                                                                                    {
                                                                                                        return dd10.Text;
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                            #endregion
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (dd9.Text.Equals(msg))
                                                                                            {
                                                                                                return dd9.Text;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    #endregion
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (dd8.Text.Equals(msg))
                                                                                    {
                                                                                        return dd8.Text;
                                                                                    }
                                                                                }
                                                                            }
                                                                            #endregion
                                                                        }
                                                                        else
                                                                        {
                                                                            if (dd7.Text.Equals(msg))
                                                                            {
                                                                                return dd7.Text;
                                                                            }
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    if (dd6.Text.Equals(msg))
                                                                    {
                                                                        return dd6.Text;
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                        }
                                                        else
                                                        {
                                                            if (dd5.Text.Equals(msg))
                                                            {
                                                                return dd5.Text;
                                                            }
                                                        }
                                                    }
                                                    #endregion
                                                }
                                                else
                                                {
                                                    if (dd4.Text.Equals(msg))
                                                    {
                                                        return dd4.Text;
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            if (dd3.Text.Equals(msg))
                                            {
                                                return dd3.Text;
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    if (dd2.Text.Equals(msg))
                                    {
                                        return dd2.Text;
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            if (dd1.Text.Equals(msg))
                            {
                                return dd1.Text;
                            }
                        }
                    }
                }
            }
            catch { }
            return string.Empty;
        }

        internal static List<Record> FindFastMessages(string msg)
        {
            List<Record> records = new List<Record>();
            try
            {
                foreach (var dd in DataFactory.Messages.Nodes)
                {
                    foreach (var dd1 in dd.SubNode)
                    {
                        if (dd1.SubNode.Count > 0)
                        {
                            #region 1
                            foreach (var dd2 in dd1.SubNode)
                            {
                                if (dd2.SubNode.Count > 0)
                                {
                                    #region 2
                                    foreach (var dd3 in dd2.SubNode)
                                    {
                                        if (dd3.SubNode.Count > 0)
                                        {
                                            #region 3
                                            foreach (var dd4 in dd3.SubNode)
                                            {
                                                if (dd4.SubNode.Count > 0)
                                                {
                                                    #region 4
                                                    foreach (var dd5 in dd4.SubNode)
                                                    {
                                                        if (dd5.SubNode.Count > 0)
                                                        {
                                                            #region 5
                                                            foreach (var dd6 in dd5.SubNode)
                                                            {
                                                                if (dd6.SubNode.Count > 0)
                                                                {
                                                                    #region 6
                                                                    foreach (var dd7 in dd6.SubNode)
                                                                    {
                                                                        if (dd7.SubNode.Count > 0)
                                                                        {
                                                                            #region 7
                                                                            foreach (var dd8 in dd7.SubNode)
                                                                            {
                                                                                if (dd8.SubNode.Count > 0)
                                                                                {
                                                                                    #region 8
                                                                                    foreach (var dd9 in dd8.SubNode)
                                                                                    {
                                                                                        if (dd9.SubNode.Count > 0)
                                                                                        {
                                                                                            #region 9
                                                                                            foreach (var dd10 in dd9.SubNode)
                                                                                            {
                                                                                                if (dd10.SubNode.Count > 0)
                                                                                                {
                                                                                                    #region 10
                                                                                                    foreach (var dd11 in dd10.SubNode)
                                                                                                    {
                                                                                                        if (dd11.SubNode.Count > 0)
                                                                                                        {
                                                                                                            #region 11
                                                                                                            foreach (var dd12 in dd11.SubNode)
                                                                                                            {
                                                                                                                if (dd12.SubNode.Count > 0)
                                                                                                                {
                                                                                                                    //temp1.Add(new Record()
                                                                                                                    //{
                                                                                                                    //    Text = temp.Text
                                                                                                                    //});
                                                                                                                }
                                                                                                                else
                                                                                                                {
                                                                                                                    if (dd12.Text.Equals(msg))
                                                                                                                    {
                                                                                                                        records.Add(new Record() { Text = dd12.Text });
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            #endregion
                                                                                                        }
                                                                                                        else
                                                                                                        {
                                                                                                            if (dd11.Text.Equals(msg))
                                                                                                            {
                                                                                                                records.Add(new Record() { Text = dd11.Text });
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    #endregion
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    if (dd10.Text.Equals(msg))
                                                                                                    {
                                                                                                        records.Add(new Record() { Text = dd10.Text });
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                            #endregion
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                            if (dd9.Text.Equals(msg))
                                                                                            {
                                                                                                records.Add(new Record() { Text = dd9.Text });
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    #endregion
                                                                                }
                                                                                else
                                                                                {
                                                                                    if (dd8.Text.Equals(msg))
                                                                                    {
                                                                                        records.Add(new Record() { Text = dd8.Text });
                                                                                    }
                                                                                }
                                                                            }
                                                                            #endregion
                                                                        }
                                                                        else
                                                                        {
                                                                            if (dd7.Text.Equals(msg))
                                                                            {
                                                                                records.Add(new Record() { Text = dd7.Text });
                                                                            }
                                                                        }
                                                                    }
                                                                    #endregion
                                                                }
                                                                else
                                                                {
                                                                    if (dd6.Text.Contains(msg))
                                                                    {
                                                                        records.Add(new Record() { Text = dd6.Text });
                                                                    }
                                                                }
                                                            }
                                                            #endregion
                                                        }
                                                        else
                                                        {
                                                            if (dd5.Text.Contains(msg))
                                                            {
                                                                records.Add(new Record() { Text = dd5.Text });
                                                            }
                                                        }
                                                    }
                                                    #endregion
                                                }
                                                else
                                                {
                                                    if (dd4.Text.Contains(msg))
                                                    {
                                                        records.Add(new Record() { Text = dd4.Text });
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            if (dd3.Text.Contains(msg))
                                            {
                                                records.Add(new Record() { Text = dd3.Text });
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    if (dd2.Text.Contains(msg))
                                    {
                                        records.Add(new Record() { Text = dd2.Text });
                                    }
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            if (dd1.Text.Contains(msg))
                            {
                                records.Add(new Record() { Text = dd1.Text });
                            }
                        }
                    }
                }
            }
            catch { }
            return records;
        }

        internal static List<Record> FindChatMessage(string msg)
        {
            List<Record> records = new List<Record>();

            return records;
        }

        internal static bool CheckHeatTime(string time)
        {
            return CheckHeatTime(DateTime.Parse(time));
        }

        internal static bool CheckHeatTime(DateTime dateTime)
        {
            if (baseDateTime.CompareTo(dateTime) > 1)
            {
                return false;
            }
            if (DateTime.Now.CompareTo(dateTime) < 1)
            {
                return true;
            }
            TimeSpan time1 = new TimeSpan(dateTime.Ticks);
            TimeSpan time2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan time = time2.Subtract(time1).Duration();
            if (time.Days > 1)
            {
                return false;
            }
            if (time.Hours > 1)
            {
                return false;
            }
            if (time.Minutes > 1)
            {
                return false;
            }
            if (time.Seconds > TIME_INTERVAL)
            {
                return false;
            }
            return true;
        }
    }
}